from xmindparser import xmind_to_dict
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from generate_case_csv_file import *
import os


# 微信生态_企微SCRM
class Generate_csv:

    def __init__(self, root):
        # GUI
        root.title('Xmind用例个数统计')
        width = 570
        height = 500
        xscreen = root.winfo_screenwidth()
        yscreen = root.winfo_screenheight()
        xmiddle = (xscreen - width) / 2
        ymiddle = (yscreen - height) / 2
        root.geometry('%dx%d+%d+%d' % (width, height, xmiddle, ymiddle))
        self.frm1 = tk.Frame(root)
        self.frm2 = tk.Frame(root)
        self.frm3 = tk.Frame(root)
        self.frm1.grid(row=0, padx='20', pady='20')
        self.frm2.grid(row=2, padx='5', pady='30')
        self.frm3.grid(row=1, padx='0', pady='0')

        self.user = Label(self.frm3, text="禅道用户名:")
        self.user.grid(row=1, column=0, padx=5, pady=5, sticky=W)
        user = StringVar()
        self.enuser = Entry(self.frm3, textvariable=user)
        self.enuser.grid(row=1, column=1, sticky='ew', columnspan=2)
        self.pwd = Label(self.frm3, text="禅道密码:")
        self.pwd.grid(row=2, column=0, padx=5, pady=5, sticky=W)
        pwd = StringVar()
        self.enpwd = Entry(self.frm3, textvariable=pwd, show='*')
        self.enpwd.grid(row=2, column=1, sticky='ew', columnspan=2)
        self.pj = Label(self.frm3, text="禅道项目:")
        self.pj.grid(row=3, column=0, padx=5, pady=5, sticky=W)
        pj = StringVar(value='微信生态_企微SCRM')  # 默认传值到输入框填写禅道项目名称
        self.enpj = Entry(self.frm3, textvariable=pj, )
        self.enpj.grid(row=3, column=1, sticky='ew', columnspan=2)

        self.but_upload = tk.Button(self.frm1, text='上传xmind文件', command=self.upload_files, bg='#dfdfdf')
        self.but_upload.grid(row=0, column=0, pady='10')
        self.text = tk.Text(self.frm1, width=55, height=10, bg='#f0f0f0')
        self.text.grid(row=1, column=0)
        self.but2 = tk.Button(self.frm2, text="转换", command=self.generate_new, bg='#dfdfdf')
        self.but2.grid(row=0, columnspan=6, pady='10')
        # self.label_file = tk.Label(self.frm2, text="文件名", relief='groove', borderwidth='2', width=25, bg='#FFD0A2')
        # self.label_file.grid(row=1, column=0)
        # self.label_pass = tk.Label(self.frm2, text="成功", relief='groove', borderwidth='2', width=10, bg='#FFD0A2').grid(row=1, column=1)

    # 用例统计表新增一行
    def generate_main(self, xmind_file, zentao_account, zentao_password, target_product_name):
        print((xmind_file, zentao_account, zentao_password, target_product_name))
        target_path = xmind_file + '.csv'
        if xmind_file[-6:] == '.xmind' and os.path.exists(xmind_file):
            generate_case(xmind_file, save_path=target_path)
        elif xmind_file[-4:] == '.csv' and os.path.exists(xmind_file):
            target_path = xmind_file
        case_modules = get_modules_from_csv(target_path)
        zentao_user = ZentaoSession()
        zentao_user.login(zentao_account, zentao_password)
        zentao_user.create_case_modules(case_modules, target_product_name)
        # 更新用例csv
        zentao_user.update_csv_case_module_id(target_path, target_product_name)

    def generate_new(self):
        lines = self.text.get(1.0, tk.END)  # 从text中获取所有行
        for xmind_file in lines.splitlines():  # 分隔成每行
            if xmind_file == '':
                break
            else:
                zentao_account = self.enuser.get()
                zentao_password = self.enpwd.get()
                target_product_name = self.enpj.get()
                self.generate_main(xmind_file, zentao_account, zentao_password, target_product_name)

    # 上传多个文件，并插入text中
    def upload_files(self):
        select_files = tk.filedialog.askopenfilenames(title="可选择1个或多个文件")
        for file in select_files:
            self.text.insert(tk.END, file + '\n')
            self.text.update()


if __name__ == '__main__':
    root = tk.Tk()
    Generate_csv(root)
    root.mainloop()
